import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/router/router.dart';

class ImageBrowsePage extends StatefulWidget {
  final List<String> images;

  final int currentIndex;

  const ImageBrowsePage({Key key, this.images, this.currentIndex})
      : super(key: key);

  @override
  _ImageBrowsePageState createState() => _ImageBrowsePageState();
}

class _ImageBrowsePageState extends State<ImageBrowsePage> {
  int curIndex;

  GlobalKey<ExtendedImageSlidePageState> slidePagekey =
      GlobalKey<ExtendedImageSlidePageState>();

  @override
  void initState() {
    super.initState();
    curIndex = widget.currentIndex;
  }

  @override
  Widget build(BuildContext context) {
    return Material(color: ThemeColors.colorType1, child: _initBody());
  }

  Widget _initBody() {
    return ExtendedImageSlidePage(
      key: slidePagekey,
      child: GestureDetector(
        child: ExtendedImage.network(
          widget.images.first,
          enableSlideOutPage: true,
          mode: ExtendedImageMode.gesture,
          initGestureConfigHandler: (ExtendedImageState state) {
            return GestureConfig(
              minScale: 0.9,
              animationMinScale: 0.7,
              maxScale: 4.0,
              animationMaxScale: 4.5,
              speed: 1.0,
              inertialSpeed: 100.0,
              initialScale: 1.0,
              inPageView: false,
              initialAlignment: InitialAlignment.center,
              gestureDetailsIsChanged: (GestureDetails details) {
                //print(details.totalScale);
              },
            );
          },

          ///make hero better when slide out
          heroBuilderForSlidingPage: (Widget result) {
            return Hero(
              tag: widget.images.first,
              child: result,
              flightShuttleBuilder: (BuildContext flightContext,
                  Animation<double> animation,
                  HeroFlightDirection flightDirection,
                  BuildContext fromHeroContext,
                  BuildContext toHeroContext) {
                final Hero hero = (flightDirection == HeroFlightDirection.pop
                    ? fromHeroContext.widget
                    : toHeroContext.widget) as Hero;
                return hero.child;
              },
            );
          },
        ),
        onTap: () {
          slidePagekey.currentState.popPage();
          LRouter.navigatorCtx(context).pop();
        },
      ),
      slideAxis: SlideAxis.both,
      slideType: SlideType.onlyImage,
    );
  }
}
